package com.ossean.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class StringHandler_ProjectName {
	
	
	
	/**
	 * Map中Integer: 0-原名;1-不含括号;2-括号中
	 * @param ospName
	 * @return
	 */
	public static Map<String,Integer> divideProjectName(String ospName){
		Map<String,Integer> result = new HashMap<String,Integer>();
		result.put(ospName, 0);
		int index1 = ospName.indexOf("(");
		if(index1 > 0){
			String inside = null;
			String outside = null;
			String str1 = ospName.substring(0, index1).trim();//“(”之前的字符串
			String str2 = "";//括号中的字符串
			String str3 = "";//括号后的字符串
			int index2 = ospName.indexOf(")");
			if(index2 > index1){
				//表示左右括号完整
				str2 = ospName.substring(index1 + 1, index2).trim();
				str3 = ospName.substring(index2 + 1).trim();
			}else{
				str2 = ospName.substring(index1 + 1).trim();
			}
			inside = (str2 + " " + str3).trim();
			outside = (str1 + " " + str3).trim();
			result.put(outside, 1);
			result.put(inside, 2);
		}else{
			index1 = ospName.indexOf("（");
			if(index1 > 0){
				String inside = null;
				String outside = null;
				String str1 = ospName.substring(0, index1).trim();//“(”之前的字符串
				String str2 = "";//括号中的字符串
				String str3 = "";//括号后的字符串
				int index2 = ospName.indexOf("）");
				if(index2 > index1){
					//表示左右括号完整
					str2 = ospName.substring(index1 + 1, index2).trim();
					str3 = ospName.substring(index2 + 1).trim();
				}else{
					str2 = ospName.substring(index1 + 1).trim();
				}
				inside = (str2 + " " + str3).trim();;
				outside = (str1 + " " + str3).trim();
				result.put(outside, 1);
				result.put(inside, 2);
			}
		}
		return result;
	}

	
	public static String removeBracket(String name){
		String result = name;
		while(result.indexOf("(")>0 || result.indexOf("（")>0)
			result=removeBracketDetail(result);
		return result;
	}
	
	public static String removeBracketDetail(String name){
		String result = name;
		int index1 = name.indexOf("(");
		if(index1 > 0){
			String outside = null;
			String str1 = name.substring(0, index1).trim();//“(”之前的字符串
			String str3 = "";//括号后的字符串
			int index2 = name.indexOf(")");
			if(index2 > index1){
				//表示左右括号完整
				str3 = name.substring(index2 + 1).trim();
			}
			outside = (str1 + " " + str3).trim();
			result = outside;
		}else{
			index1 = name.indexOf("（");
			if(index1 > 0){
				String outside = null;
				String str1 = name.substring(0, index1).trim();//“(”之前的字符串
				String str3 = "";//括号后的字符串
				int index2 = name.indexOf("）");
				if(index2 > index1){
					//表示左右括号完整
					str3 = name.substring(index2 + 1).trim();
				}
				outside = (str1 + " " + str3).trim();
				result = outside;
			}
		}
		return result;
	}
	/**
	 * 提取项目名括号中的描述信息 如果没有括号 返回""
	 */
	public static String getBracket(String name){
		
		int index1 = name.lastIndexOf("(");
		int index2 = -1;
		String inside = null;
		if(index1 > 0){
			index2 = name.lastIndexOf(")");
			if(index2 > index1)
				inside = name.substring(index1 + 1, index2);//抽取括号内部的信息
		}else{
			index1 = name.lastIndexOf("（");
			if(index1 > 0){
				index2 = name.lastIndexOf("）");
				if(index2 > index1)
					inside = name.substring(index1 + 1, index2);
			}
			else{
				index1 = name.lastIndexOf("[");
				if(index1 > 0){
					index2 = name.lastIndexOf("]");
					if(index2 > index1)
						inside = name.substring(index1 + 1, index2);
				}else{
					index1 = name.lastIndexOf("{");
					if(index1 > 0){
						index2 = name.lastIndexOf("}");
						if(index2 > index1)
							inside = name.substring(index1 + 1, index2);
					}else{
						index1 = name.lastIndexOf("【");
						if(index1 > 0){
							index2 = name.lastIndexOf("】");
							if(index2 > index1)
								inside = name.substring(index1 + 1, index2);
						}
					}
				}
			}
		}
		return inside;
	}
	
	/**
	 * 提取项目名中除去公司名的信息 作为别名
	 */
	public static String getProjectWithoutComName(String name){
		String result = name;
		//读取需要去除的公司名信息
		List<String> companyList = FileReader.read("./files/companyName.txt");
		for(String company : companyList){
			name = name.trim();
			int index = name.toLowerCase().indexOf(company);
			if(index == 0){
				//表示找到了子字符串 并且在开头位置
				name = name.substring(company.length());
				result = name.trim();
				break;
			}
		}
		
		return result;
	}
	
	
	/**
	 * 判断项目名是由几个关键词组成的
	 */
	public static int getWordNum(String name){
		String[] wordArray = name.split(" ");
		return wordArray.length;
	}
	
}
